package controllers;

import java.util.ArrayList;

import models.Event;
import play.data.validation.Required;
import play.mvc.Controller;
import play.mvc.With;

@With(Secure.class)
public class Events extends Controller {

	public static void AddEvent(@Required String Title,
			@Required String Description) {
		if (!validation.hasErrors()) {
			ArrayList<Event> listevent = (ArrayList) Event.findAll();
			for (int i = 0; i < listevent.size(); i++) {

				if (listevent.get(i).Title.equalsIgnoreCase(Title)) {
					flash.error("OoOps there is already an event with the same title. Please Change it");
					render();
				}

			}
			Event event = new Event(Title, Description);
			event.save();
			CreatedEvent(event);

		} else {
			render();
		}
	}

	public static void CreatedEvent(Event event) {
		render(event);

	}

	public static void ViewAllEvents() {
		ArrayList eventlist = (ArrayList) Event.findAll();
		render(eventlist);
	}

	public static void ViewEvent(long id) {
		Event x = Event.findById(id);
		render(x);

	}

	public static void DeleteEvent(long id) {
		Event event = Event.findById(id);
		event.delete();
		ViewAllEvents();
	}

	public static void EditEvent(long id) {
		Event vana = Event.findById(id);
		render(vana, id);
	}

	public static void Edit(String Title, String Description, long id) {
		Event vana = Event.findById(id);
		vana.Title = Title;
		vana.Description = Description;
		vana.save();
		ViewEvent(id);

	}
}
